package member.json;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import member.vo.ZipCodeVO;

import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.web.servlet.view.AbstractView;

public class JSonView extends AbstractView {

	@Override
	protected void renderMergedOutputModel(Map map,
			HttpServletRequest arg1, HttpServletResponse response) throws Exception {
		ArrayList list = new ArrayList();
		Set set = map.keySet();
		Iterator itr = set.iterator();
		list = (ArrayList)map.get(itr.next());
		JSONArray array = new JSONArray();
		for(int i=0; i<list.size();i++){
			JSONObject jObj=new JSONObject();
			ZipCodeVO dto =(ZipCodeVO)list.get(i);
			jObj.put("zipcode1", dto.getZipcode1());
			jObj.put("zipcode2",dto.getZipcode2());
			String addressDetail = dto.getSido()+" "+dto.getGugun()+" "+dto.getDong();
			if(dto.getRi()!=null){
				addressDetail = addressDetail+" "+dto.getRi();
			}
			if(dto.getBldg()!=null){
				addressDetail = addressDetail+" "+dto.getBldg();
			}
			jObj.put("addressDetail", addressDetail);
			array.put(jObj);
		}
		response.setContentType("text/html;charset=UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		out.println(array.toString());
		out.close();
	}

}
